package com.arica.framework.manager.pool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author soo
 * @title: SooThreadPoolTaskExecutor
 * @projectName 611
 * @description: 定义线程池
 * @date 2019/11/2917:41
 */
public class SooThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
    private static final Logger log = LoggerFactory.getLogger("soo-thread-pool");

    /**
     * 打印线程池状态
     */
    private void showThreadPoolInfo(String prefix) {
        ThreadPoolExecutor executor = getThreadPoolExecutor();
        log.info("{},{},taskCount:[{}],completedTaskCount:[{}],acviveCount:[{}],queueSize:[{}]",
                this.getThreadNamePrefix()
                ,prefix
                ,executor.getTaskCount()
                ,executor.getCompletedTaskCount()
                ,executor.getActiveCount()
                ,executor.getQueue().size()
        );
    }

    @Override
    public void execute(Runnable task) {
        showThreadPoolInfo("soo-pool execute");
        super.execute(task);
    }

    @Override
    public void execute(Runnable task, long startTimeout) {
        showThreadPoolInfo("soo-pool execute with timeout");
        super.execute(task, startTimeout);
    }

    @Override
    public Future<?> submit(Runnable task) {
        showThreadPoolInfo("soo-pool submit with callable");
        return super.submit(task);
    }

}
